unit StringSplitTest;

interface
uses Classes, SysUtils, Windows;

  procedure Split(const Text: string; Words: TStrings; Delimeters: string = '');

  procedure SplitTest;

implementation

  procedure Split(const Text: string; Words: TStrings; Delimeters: string = '');
  var i: integer;
      s3: string;
      wStart, wLength: integer;
  begin
      s3:=Delimeters+' '+#13#10;
      Words.Clear;
      wStart := 1;
      wLength := 0;
      for i:=1 to length(Text) do
      begin
          if (Pos(Text[i], s3) > 0) then
          begin
            if (wLength > 0) then Words.Add(Copy(Text, wStart, wLength));
            wStart := i + 1;
            wLength := 0;
          end else
            Inc(wLength);
      end;
      if wLength > 0 then Words.Add(Copy(Text, wStart, wLength));
  end;

  procedure GetWords(s: string; Words: TStrings; Delimeters: string = '');
  var i: integer;
      s2,s3: string;
  begin
      s3:=Delimeters+' '+#13#10;
      s2:=''; Words.Clear;
      for i:=1 to length(s) do
      begin
          if (Pos(s[i],s3)>0) and (s2<>'') then begin Words.Add(s2); s2:=''; end;
          if Pos(s[i],s3)<=0 then s2:=s2+s[i];
      end;
      if s2<>'' then Words.Add(s2);
  end;

  procedure SplitTest;
  const text: string = 'egf awegae gaeg aegaae gae gaeg ae';
        testCount: integer = 100000;
  var st: TStrings;
      i: integer;
      lastTick: DWord;
  begin
    st := TStringList.Create;
    try
      lastTick := GetTickCount;
      for i := 0 to testCount - 1 do Split(text, st);
      WriteLn('Split test = ' + IntToStr(GetTickCount - lastTick));

      lastTick := GetTickCount;
      for i := 0 to testCount - 1 do GetWords(text, st);
      WriteLn('GetWords test = ' + IntToStr(GetTickCount - lastTick));
    finally
      st.Free;
    end;
  end;

end.
